var __assign =
  (this && this.__assign) ||
  function () {
    __assign =
      Object.assign ||
      function (t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
          s = arguments[i];
          for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
        }
        return t;
      };
    return __assign.apply(this, arguments);
  };
var __spreadArray =
  (this && this.__spreadArray) ||
  function (to, from, pack) {
    if (pack || arguments.length === 2)
      for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
          if (!ar) ar = Array.prototype.slice.call(from, 0, i);
          ar[i] = from[i];
        }
      }
    return to.concat(ar || Array.prototype.slice.call(from));
  };
import { Col, Form } from 'antd';
import axios from 'axios';
import React, { useEffect, useMemo, useState } from 'react';
import { leggoItemStore } from '../itemStore';
import { Leggo } from '../utils/Leggo';
export default function LeggoItem(props) {
  var _a, _b, _c, _d;
  var leggo = props.leggo,
    schema = props.schema,
    schemaList = props.schemaList;
  var type = schema.type,
    formType = schema.formType,
    configs = schema.configs,
    needDefineGetterProps = schema.needDefineGetterProps;
  var itemProps = configs.itemProps,
    inputProps = configs.inputProps,
    extra = configs.extra,
    postman = configs.postman,
    Successor = configs.Successor,
    SuperSuccessor = configs.SuperSuccessor;
  var postmanParamsValueList =
    ((_a = postman === null || postman === void 0 ? void 0 : postman.params) === null ||
    _a === void 0
      ? void 0
      : _a.map(function (item) {
          return item.value;
        })) || [];
  var postmanDataValueList =
    ((_b = postman === null || postman === void 0 ? void 0 : postman.data) === null || _b === void 0
      ? void 0
      : _b.map(function (item) {
          return item.value;
        })) || [];
  var StandardInput =
    ((_c = leggoItemStore.total[formType]) === null || _c === void 0 ? void 0 : _c.StandardInput) ||
    function () {
      return React.createElement('div', null);
    };
  var rules = Leggo.createRules(
    itemProps.rules,
    extra === null || extra === void 0 ? void 0 : extra.wordsLimit
  );
  var children = Leggo.createChildren(
    extra === null || extra === void 0 ? void 0 : extra.childrenNode
  );
  var _e = useState(0),
    setForceRender = _e[1];
  useMemo(
    function () {
      schema.forceLeggoFormItemRender = function () {
        return setForceRender(function (pre) {
          return pre + 1;
        });
      };
      Object.values(needDefineGetterProps).forEach(function (getterInfo) {
        var observedStringedName = getterInfo.observedStringedName,
          namepath = getterInfo.namepath,
          publicStateKey = getterInfo.publicStateKey,
          reference = getterInfo.reference,
          rule = getterInfo.rule;
        var isFromPublicStates = observedStringedName === 'publicStates';
        var linkedSchema = schemaList.find(function (schema) {
          return schema.getStringedName() === observedStringedName;
        });
        var targetProp = namepath.slice(0, -1).reduce(function (pre, cur) {
          return pre[cur];
        }, configs);
        var targetKey = namepath.slice(-1)[0];
        var targetType = typeof targetProp[targetKey];
        !isFromPublicStates && linkedSchema.linkingStringedNames.add(schema.getStringedName());
        Reflect.defineProperty(targetProp, targetKey, {
          set: function () {
            return null;
          },
          get: function () {
            if (targetKey === 'disabled' && leggo.allDisabledIsLockedToTrue) {
              return true;
            }
            var targetValue =
              linkedSchema === null || linkedSchema === void 0
                ? void 0
                : linkedSchema.currentItemValue;
            if (isFromPublicStates) {
              var publicState = leggo.publicStates[publicStateKey];
              return typeof publicState === 'function' ? publicState() : publicState;
            }
            if (reference && rule) {
              targetValue =
                targetValue === null || targetValue === void 0 ? void 0 : targetValue.toString();
              switch (rule) {
                case '<':
                  targetValue = targetValue < reference;
                  break;
                case '<=':
                  targetValue = targetValue <= reference;
                  break;
                case '===':
                  targetValue = targetValue === reference;
                  break;
                case '!==':
                  targetValue = targetValue !== reference;
                  break;
                case '>=':
                  targetValue = targetValue >= reference;
                  break;
                case '>':
                  targetValue = targetValue > reference;
              }
            } else {
              switch (targetType) {
                case 'boolean':
                  targetValue = Boolean(targetValue);
                  break;
                case 'number':
                  targetValue = Number(targetValue);
                  break;
                case 'string':
                  targetValue =
                    targetValue === null || targetValue === void 0
                      ? void 0
                      : targetValue.toString();
              }
            }
            return rule === '!' ? !targetValue : targetValue;
          }
        });
      });
    },
    [leggo.forceRenderMark]
  );
  useEffect(function () {
    var _a = postman || {},
      method = _a.method,
      url = _a.url,
      params = _a.params,
      data = _a.data,
      responseNamepath = _a.responseNamepath;
    if (method && url) {
      var paramsParsed =
        params === null || params === void 0
          ? void 0
          : params.reduce(function (pre, cur) {
              pre[cur.key] = cur.value === '' ? undefined : cur.value;
              return pre;
            }, {});
      var dataParsed =
        data === null || data === void 0
          ? void 0
          : data.reduce(function (pre, cur) {
              pre[cur.key] = cur.value;
              return pre;
            }, {});
      axios({ method: method, url: url, params: paramsParsed, data: dataParsed }).then(function (
        res
      ) {
        var targetValue = responseNamepath
          .split('.')
          .slice(1)
          .reduce(function (pre, cur) {
            return pre === null || pre === void 0 ? void 0 : pre[cur];
          }, res);
        configs.inputProps.options = targetValue;
        setForceRender(function (pre) {
          return pre + 1;
        });
      });
    }
  }, __spreadArray(__spreadArray([], postmanParamsValueList, true), postmanDataValueList, true));
  return SuperSuccessor
    ? React.createElement(SuperSuccessor, null)
    : Successor
    ? React.createElement(
        Col,
        { span: itemProps.layout || 8 },
        React.createElement(
          Form.Item,
          {
            label: itemProps.label,
            required:
              (_d = rules === null || rules === void 0 ? void 0 : rules[0]) === null ||
              _d === void 0
                ? void 0
                : _d.required
          },
          React.createElement(
            Successor,
            null,
            React.createElement(
              Form.Item,
              __assign({}, itemProps, { rules: rules, noStyle: true }),
              React.createElement(StandardInput, __assign({}, inputProps), children)
            )
          )
        )
      )
    : React.createElement(
        Col,
        { span: itemProps.layout || 8 },
        React.createElement(
          Form.Item,
          __assign({}, itemProps, { rules: rules }),
          React.createElement(StandardInput, __assign({}, inputProps), children)
        )
      );
}
